#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright (c) 2022 Institute of Software, CAS
# Author: xiaofan@iscas.ac.cn

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
import os
import os.path
import subprocess
import multiprocessing
import glob
import shutil
from meson_cross_process import prepare_environment

# input /path/to/openharmony/out/rk3568

nproc = multiprocessing.cpu_count()
out_dir = sys.argv[1]
glmark2_install_dir = os.path.join(out_dir, 'packages', 'phone', 'glmark2')
glmark2_data_install_dir = os.path.join(out_dir, 'packages', 'phone', 'glmark2', 'glmark2')
product = os.path.basename(out_dir)
project_dir = os.path.dirname(os.path.dirname(out_dir))
project_dir = os.path.abspath(project_dir)
glmark2_dir = os.path.dirname(os.path.dirname(__file__))
glmark2_dir = os.path.abspath(glmark2_dir)
pkgconf_dir = os.path.join(glmark2_dir, './pkgconfig')
os.environ['PKG_CONFIG_PATH'] = pkgconf_dir

# workaround: using system python instead of prebuilt python
path_old = os.environ['PATH'].split(':')
path_filter = lambda p: not p.endswith('/prebuilts/python/linux-x86/3.8.5/bin')
path_new = ':'.join(filter(path_filter, path_old))
os.environ['PATH'] = path_new

os.chdir(glmark2_dir)
prepare_environment(project_dir, product)

meson_cmd = [
    'meson',
    'setup',
    glmark2_dir,
    'build-ohos',
    '-Dflavors=ohos-glesv2', 
    '--cross-file=cross_file',
    F'--prefix={glmark2_dir}/build-ohos/install',
]

subprocess.run(meson_cmd, check=True)
subprocess.run(F'ninja -C build-ohos -j{nproc}', shell=True, check=True)
subprocess.run(F'ninja -C build-ohos install', shell=True, check=True)

build_lib = os.path.join(glmark2_dir, 'build-ohos', 'install', 'bin')
glmark2_data_build_dir = os.path.join(glmark2_dir, 'build-ohos', 'install', 'share', 'glmark2')

#install to out/adl/packages/phone/glmark2
shutil.rmtree(glmark2_install_dir, ignore_errors=True)
shutil.rmtree(glmark2_data_install_dir, ignore_errors=True)
os.makedirs(glmark2_install_dir, exist_ok=True)
for item in glob.glob(os.path.join(build_lib, 'glmark2*')):
    shutil.copy(item, glmark2_install_dir)

shutil.copytree(glmark2_data_build_dir, glmark2_data_install_dir, dirs_exist_ok=True)
